import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * gets all of the specifications needed before downloading the file (destination ant type)
 *
 */
public class Specification extends JDialog {

	private static final long serialVersionUID = 1L;
	/** window panel */
	private JPanel jContentPane = null;
	/** window title label */
	private JLabel titleLabel = null;
	/** language label */
	private JLabel lanLabel = null;
	/** format type label */
	private JLabel formatLabel = null;
	/** destination label */
	private JLabel destLabel = null;
	/** language disabled text field */
	private JTextField language = null;
	/** format type combo box */
	private JComboBox format = null;
	/** destination folder text field */
	private JTextField dest = null;
	/** browse for destination text field */
	private JButton browse = null;
	/** get book button */
	private JButton get = null;
	/** go to main screen button */
	private JButton back = null;
	/** holds file language */
	public String lan;
	/** holds available file format types */
	public String[] formatSelection;
	/** holds book name */
	public String bookName;
	/** holds book serial */
	public String serial;
	/** book name label */
	private JLabel nameLabel = null;
	/** book name */
	private JTextField book = null;
	/** details label */
	private JLabel details = null;
	

	/**
	 * initialize the window, show book name, language and available format types for this book
	 * @param owner
	 */
	public Specification(Frame owner,String name,String s,String l,Boolean[] format) 
	{
		super(owner);
		int count=0;
		for(int i=0;i<format.length;i++)
		{
			if(format[i]) count++;
		}
		formatSelection=new String[count];
		int j=0;
		if(format[0]) formatSelection[j++]="pdf";
		if(format[1]) formatSelection[j++]="doc";
		if(format[2]) formatSelection[j]="fb2";
		lan=l;
		serial=s;
		bookName=name;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(446, 348);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			details = new JLabel();
			details.setBounds(new Rectangle(142, 240, 105, 19));
			details.setText("( path + filename)");
			nameLabel = new JLabel();
			nameLabel.setBounds(new Rectangle(16, 77, 76, 19));
			nameLabel.setText("Book Name:");
			destLabel = new JLabel();
			destLabel.setBounds(new Rectangle(17, 206, 83, 31));
			destLabel.setText("Destination:");
			formatLabel = new JLabel();
			formatLabel.setBounds(new Rectangle(16, 163, 80, 25));
			formatLabel.setText("Format:");
			lanLabel = new JLabel();
			lanLabel.setBounds(new Rectangle(17, 115, 80, 25));
			lanLabel.setText("Language:");
			titleLabel = new JLabel();
			titleLabel.setBounds(new Rectangle(99, 7, 194, 48));
			titleLabel.setText("Specifications");
			titleLabel.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,30));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(titleLabel, null);
			jContentPane.add(lanLabel, null);
			jContentPane.add(formatLabel, null);
			jContentPane.add(destLabel, null);
			jContentPane.add(getLanguage(), null);
			jContentPane.add(getFormat(), null);
			jContentPane.add(getDest(), null);
			jContentPane.add(getBrowse(), null);
			jContentPane.add(getGet(), null);
			jContentPane.add(getBack(), null);
			jContentPane.add(nameLabel, null);
			jContentPane.add(getBook(), null);
			jContentPane.add(details, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes language	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLanguage() {
		if (language == null) {
			language = new JTextField();
			language.setText(lan);
			language.setBounds(new Rectangle(109, 116, 199, 21));
			language.setEnabled(false);
		}
		return language;
	}

	/**
	 * This method initializes format	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getFormat() {
		if (format == null) {
			format = new JComboBox(formatSelection);
			format.setBounds(new Rectangle(118, 165, 109, 19));
		}
		return format;
	}

	/**
	 * This method initializes dest	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDest() {
		if (dest == null) {
			dest = new JTextField();
			dest.setBounds(new Rectangle(106, 210, 202, 23));
		}
		return dest;
	}

	/**
	 * This method initializes browse and adds listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBrowse() {
		if (browse == null) {
			browse = new JButton("Browse");
			browse.setBounds(new Rectangle(322, 205, 102, 31));
			browse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					final JFileChooser fc=new JFileChooser();
					String path="";
					JList list=new JList();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					if(JFileChooser.APPROVE_OPTION==fc.showOpenDialog(list))
					{	
							path =fc.getSelectedFile().getPath();
						if(!path.contains("."+(String)format.getSelectedItem()))
							path+="."+(String)format.getSelectedItem();
						
					}	
					dest.setText(path);
											
				}
			});
		}
		return browse;
	}

	/**
	 * This method initializes get and adds listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getGet() {
		if (get == null) {
			get = new JButton("Get File");
			get.setBounds(new Rectangle(240, 271, 121, 31));
			get.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					Main.myBooksControl.transfer((String)format.getSelectedItem(), serial, lan,dest.getText());
					dispose();
				}
			});
		}
		return get;
	}

	/**
	 * This method initializes back	and adds listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBack() {
		if (back == null) {
			back = new JButton("Back");
			back.setBounds(new Rectangle(43, 271, 123, 29));
			back.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return back;
	}

	/**
	 * This method initializes book	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getBook() {
		if (book == null) {
			book = new JTextField();
			book.setBounds(new Rectangle(109, 77, 201, 21));
			book.setText(bookName);
			book.setEnabled(false);
		}
		return book;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
